package com.acme.ioniolab.serialization.implementations;

import com.acme.ioniolab.serialization.SensorData;
import com.acme.ioniolab.serialization.Serializer;
import com.acme.ioniolab.serialization.implementations.SensorDataProto.SensorDataBatch;
import com.google.gson.Gson;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;

public class ProtobufSerializer implements Serializer {
	
	@Override
	public String getType() {
		return "proto";
	}
	
	SensorDataBatch.SensorData.Builder builder = SensorDataBatch.SensorData.newBuilder();

    @Override
    public Object serialize(Object object) {
        SensorData sensorData = (SensorData) object;
        builder.setAltitude(sensorData.getAltitude());
        builder.setLatitude(sensorData.getLatitude());
        builder.setLongitude(sensorData.getLongitude());
        builder.setTemp(sensorData.getTemp());
        return builder.build().toByteArray();
    }

    @Override
    public Object deserialize(Object object) {
            SensorDataBatch.SensorData pSensorData;
			try {
				pSensorData = SensorDataBatch.SensorData.parseFrom((byte[]) object);
				return new SensorData(pSensorData.getLongitude(), pSensorData.getLatitude(), pSensorData.getAltitude(), pSensorData.getTemp());
			} catch (InvalidProtocolBufferException e) {
				e.printStackTrace();
			}
			return null;
    }

}
